package com.jaya.budan.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.jaya.budan.R;
import com.jaya.budan.adapter.Fill_Information2_Adapter;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.bean.User_Information;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.thirdpush.GenerateTestUserSig;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;
import com.jaya.budan.views.MyGridView;
import com.tencent.imsdk.TIMCallBack;
import com.tencent.imsdk.TIMManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Fill_Information3_Activity extends BaseActivity implements View.OnClickListener {

    private Button btn_fill_informat3_success;
    private RelativeLayout back;
    private MyGridView gv_fill_item_type, gv_fill_item_tixing, gv_fill_item_style;
    private TextView tv_fill_info2_tixing;
    private String nicename, height = "170cm", weight = "65kg", time_bron = "1990-01-01", access_token;
    private int sex = 1, int_type = 0, int_juese = 0, int_tixing = 0;
    private Map<Integer, String> list_aim, list_tixing, list_style;
    private List<Integer> list_style2;
    private User_Information user_information;
    private Fill_Information2_Adapter fill_information2_adapter, fill_information2_adapter3, fill_information2_adapter4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fill_information3);
        Intent intent = getIntent();
        nicename = intent.getStringExtra("nicename");
        sex = intent.getIntExtra("sex", 1);
        time_bron = intent.getStringExtra("birthday");
        height = intent.getStringExtra("height");
        weight = intent.getStringExtra("weigh");
        access_token = intent.getStringExtra("access_token");
        int_type = intent.getIntExtra("int_type", 1);
        int_juese = intent.getIntExtra("int_juese", 1);
        int_tixing = intent.getIntExtra("int_tixing", 1);
        list_style2 = (List<Integer>) intent.getSerializableExtra("list_style2");
        user_information = (User_Information) intent.getSerializableExtra("user_information");
        back = findViewById(R.id.back);
        tv_fill_info2_tixing = findViewById(R.id.tv_fill_info2_tixing);
        gv_fill_item_type = findViewById(R.id.gv_fill_item_type);
        gv_fill_item_tixing = findViewById(R.id.gv_fill_item_tixing);
        gv_fill_item_style = findViewById(R.id.gv_fill_item_style);
        btn_fill_informat3_success = findViewById(R.id.btn_fill_informat3_success);
        list_aim = new HashMap<>();
        list_tixing = new HashMap<>();
        list_style = new HashMap<>();
        btn_fill_informat3_success.setOnClickListener(this);
        back.setOnClickListener(this);
        gv_fill_item_type.setVerticalSpacing(15);//设置item行间距
        gv_fill_item_type.setHorizontalSpacing(15);
        gv_fill_item_tixing.setVerticalSpacing(15);
        gv_fill_item_tixing.setHorizontalSpacing(15);
        gv_fill_item_style.setVerticalSpacing(15);
        gv_fill_item_style.setHorizontalSpacing(15);
        if (user_information != null){
            fill_information2_adapter = new Fill_Information2_Adapter(this, user_information, 6);
            fill_information2_adapter3 = new Fill_Information2_Adapter(this, user_information, 3);
            fill_information2_adapter4 = new Fill_Information2_Adapter(this, user_information, 4);
            gv_fill_item_type.setAdapter(fill_information2_adapter);
            gv_fill_item_tixing.setAdapter(fill_information2_adapter3);
            gv_fill_item_style.setAdapter(fill_information2_adapter4);
            gv_fill_item_type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    RelativeLayout rl_fill_gv_item = view.findViewById(R.id.rl_fill_gv_item);
                    // 选中的值
                    Integer key = user_information.getAim().get(position).getValue();
                    String value = list_aim.get(key);
                    if(ToolUtils.isEmpty(value)){ // 没选中
//                    ToolUtils.showToast(Fill_Information3_Activity.this, user_information.getAim().get(position).getName());
                        rl_fill_gv_item.setBackgroundResource(R.drawable.login_login);
                        list_aim.put(key,"1");
                        btn_fill_informat3_success.setEnabled(true);
                    } else {
                        list_aim.remove(key);
                        rl_fill_gv_item.setBackgroundResource(R.drawable.login_exit);
//                    ToolUtils.showToast(Fill_Information3_Activity.this, user_information.getAim().get(position).getName() + "-取消");
                        btn_fill_informat3_success.setEnabled(false);
                    }
                }
            });
            gv_fill_item_tixing.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                fill_information2_adapter3.setSelect(position);
//                fill_information2_adapter3.notifyDataSetChanged();
                    RelativeLayout rl_fill_gv_item = view.findViewById(R.id.rl_fill_gv_item);
                    // 选中的值
                    Integer int_i_face = user_information.getFace().get(position).getValue();
                    String value = list_tixing.get(int_i_face);
                    if(ToolUtils.isEmpty(value)){ // 没选中
//                    ToolUtils.showToast(Fill_Information3_Activity.this, user_information.getFace().get(position).getName());
                        rl_fill_gv_item.setBackgroundResource(R.drawable.login_login);
                        list_tixing.put(int_i_face, "1");
                    } else {
                        list_tixing.remove(int_i_face);
                        rl_fill_gv_item.setBackgroundResource(R.drawable.login_exit);
//                    ToolUtils.showToast(Fill_Information3_Activity.this, user_information.getFace().get(position).getName() + "-取消");
                    }
                }
            });
            gv_fill_item_style.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    RelativeLayout rl_fill_gv_item = view.findViewById(R.id.rl_fill_gv_item);
                    // 选中的值
                    Integer key = user_information.getStyle().get(position).getValue();
                    String value = list_style.get(key);
                    if(ToolUtils.isEmpty(value)){ // 没选中
//                    ToolUtils.showToast(Fill_Information3_Activity.this, user_information.getStyle().get(position).getName());
                        rl_fill_gv_item.setBackgroundResource(R.drawable.login_login);
                        list_style.put(key, "1");
                    } else {
                        list_style.remove(key);
                        rl_fill_gv_item.setBackgroundResource(R.drawable.login_exit);
//                    ToolUtils.showToast(Fill_Information3_Activity.this, user_information.getStyle().get(position).getName() + "-取消");
                    }
                }
            });
        }
        if (sex == 2){
            tv_fill_info2_tixing.setText("形象");
        }
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_fill_informat3_success:
                SharedPreferences preferences = getSharedPreferences("gd_amapLocation", Context.MODE_PRIVATE);
                JSONArray list_aim2 = new JSONArray();
                JSONArray list_i_face = new JSONArray();
                JSONArray list_i_style = new JSONArray();
                JSONArray list_style3 = new JSONArray();
                for(Map.Entry<Integer, String> entry : list_aim.entrySet()){
                    list_aim2.put(entry.getKey());
                }
                for(Map.Entry<Integer, String> entry : list_tixing.entrySet()){
                    list_i_face.put(entry.getKey());
                }
                for(Map.Entry<Integer, String> entry : list_style.entrySet()){
                    list_i_style.put(entry.getKey());
                }
                list_style3.put(list_style2);
                int hei = Integer.parseInt(height.substring(0, 3));
                int wei = Integer.parseInt(weight.substring(0, 2));
                JSONObject object = new JSONObject();
                try {
                    object.put("nicename", nicename);
                    object.put("sex", sex);
                    object.put("birthday", time_bron);
                    object.put("height", hei);
                    object.put("weigh", wei);
                    object.put("longitude", preferences.getString("longitude", ""));//经度
                    object.put("latitude", preferences.getString("latitude", ""));//纬度
                    object.put("is_face", 1);//纯脸模式 1是 0否
                    object.put("remark", "");//签名
                    object.put("type", int_type);//类型
                    object.put("role", int_juese);//角色
                    object.put("face", int_tixing);//形象
                    object.put("status", 0);//情感状态
                    object.put("style", list_style3);//个性特点
                    object.put("aim", list_aim2);//目的
                    object.put("i_face", list_i_face);//喜欢的形象
                    object.put("i_style", list_i_style);//喜欢的个性特点
                    object.put("country", 0);//国家
                    object.put("province", 0);//省份
                    object.put("city", 0);//城市
                    object.put("district", 0);//地区
                    object.put("work", "");//工作行业
                    object.put("blood", "");//血型
                    object.put("identity", "");//身份
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                postData(object);
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postData(JSONObject object) {
        Log.d("fill_information", object.toString());
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, RequestConnect.fill_information_Url + access_token, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("fill_information", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONObject info = jsonObject.getJSONObject("info");
                        int id = info.getInt("uid");
                        // 获取userSig函数
                        String userSig = GenerateTestUserSig.genTestUserSig(id + "");
                        TIMManager.getInstance().login(id + "", userSig, new TIMCallBack() {
                            @Override
                            public void onError(int code, String desc) {
                                ToolUtils.showToast(Fill_Information3_Activity.this, "注册失败, errCode = " + code + ", errInfo = " + desc);
                            }

                            @Override
                            public void onSuccess() {
                                Log.d("fill_information", "注册成功");
                            }
                        });
                        ToolUtils.showToast(Fill_Information3_Activity.this, message);
                        Intent intent = new Intent(Fill_Information3_Activity.this, MainActivity.class);//资料填写完毕，跳转至首页
                        intent.putExtra("access_token", access_token);
                        startActivity(intent);
                        finish();
                    } else {
                        ToolUtils.showToast(Fill_Information3_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(Fill_Information3_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }
}
